package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lanhaif
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/eduservice/eduvideo")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

//    注入client
    @Autowired
    private VodClient vodClient;


//    添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        try {
            eduVideoService.save(eduVideo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"添加小节异常");
        }
        return R.ok();
    }


//    删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        try {
            EduVideo byId = eduVideoService.getById(id);
            if (!StringUtils.isEmpty(byId.getVideoSourceId())){
                vodClient.removeAlyVideo(byId.getVideoSourceId());
            }
            eduVideoService.removeById(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"删除小节异常");
        }

    }


//    修改小节 TODO
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        try {
            eduVideoService.updateById(eduVideo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"更新小节异常");
        }
        return R.ok();
}

    @GetMapping("findVideoById/{id}")
    public R  findVideoById(@PathVariable String id){
        try {
            EduVideo eduVideo=eduVideoService.findVideoById(id);
            return R.ok().data("eduVideo",eduVideo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"查询小节异常");
        }
    }

}

